<?php
/**
 * This class extends Model class, using for customer default Model behavior
 *
 * @author Dat Nguyen <datnt@evolable.asia>
 */
class AppModel extends Model {
	const THUMB_SUFIX = '_thumb';
	/*
	 * alphaNumericDashUnderscore validate
	 */
	function alphaNumericDashUnderscore($check) {
      // $data array is passed using the form field name as the key
      // have to extract the value to make the function generic
      $value = array_shift($check);
      return preg_match('|^[0-9a-zA-Z_-]*$|', $value);
    }

	/**
	 * Compare two field, validate rule
	 * @param array $field
	 * @param null $compareField
	 * @return bool
	 */
	function compareField($field = array(), $compareField = null) {
		foreach ($field as $key => $value) {
			if (isset ($this->data[$this->name][$compareField])) {
				$value2 = $this->data[$this->name][$compareField];
			} else {
				$value2 = '';
			}
			if ($value == $value2) {
				return true;
			}
		}
		return false;
	}
	
	/**
	/**
	 * Checks if the given password is correct.
	 * @param string the password to be validated
	 * @return boolean whether the password is valid
	 */
	public function validatePassword($inputPassword, $dbPassword) {
		return crypt($inputPassword, $dbPassword) === $dbPassword;
	}

	/**
	 * Generates the password hash.
	 * @param string password
	 * @return string hash
	 */
	public function hashPassword($password) {
		return crypt($password, $this->generateSalt());
	}

	protected function generateSalt($cost = 10) {
		if (!is_numeric($cost) || $cost < 4 || $cost > 31) {

		}
		// Get some pseudo-random data from mt_rand().
		$rand = '';
		for ($i = 0; $i < 8; ++$i)
			$rand .= pack('S', mt_rand(0, 0xffff));
		// Add the microtime for a little more entropy.
		$rand .= microtime();
		// Mix the bits cryptographically.
		$rand = sha1($rand, true);
		// Form the prefix that specifies hash algorithm type and cost parameter.
		$salt = '$2a$' . str_pad((int)$cost, 2, '0', STR_PAD_RIGHT) . '$';
		// Append the random salt string in the required base64 format.
		$salt .= strtr(substr(base64_encode($rand), 0, 22), array('+' => '.'));
		return $salt;
	}

	/**
	 * Generate crypt file name and return the array with name and extension
	 *
	 * @param $fileName
	 * @return array (filename, extension)
	 */
	function hashFileName($fileName) {
		if (preg_match('/([^\/\?\*\\:;{}]+)\.([a-z0-9]+)$/i', $fileName, $matches)) {
			return array(sha1(uniqid($fileName)), $matches[2]);
		}
	}

	/**
	 * Get original file name from the thumbnail file name
	 *
	 * @param $fileName
	 * @return mixed The original file name
	 */
	function getOriginalFileName($fileName) {
		return str_replace(self::THUMB_SUFIX, '', $fileName);
	}
	
	/**
	 * check some special characters
	 * @param 
	 * 
	 */
	function specialCharacters($check) {
		$value = array_shift($check);
		return !preg_match('{[\<\>~$$?&`!#^%\{\}()]}', $value);
	}
	
	/**
	 * update del_flg =1 by id
	 * @param type $id
	 * @return type
	 */
	function deleteDataById($id) {
		$updateFields = array(
			$this->alias . '.del_flg' => 1,
			$this->alias . '.modified' => "'" . date('Y-m-d H:i:s') . "'",
		);
		$conditions = array(
			$this->alias . '.id' => $id
		);
		return $this->updateAll($updateFields, $conditions);
	}
	
	/**
	 * 
	 * @param string $key
	 * @param type $value
	 * @return type
	 */
	function deleteDataByKey($key, $value) {
		$updateFields = array(
			$this->alias . '.del_flg' => 1,
			$this->alias . '.modified' => "'" . date('Y-m-d H:i:s') . "'",
		);
		$conditions = array(
			$this->alias . '.'. $key => $value,
			$this->alias . '.del_flg' => 0
		);
		return $this->updateAll($updateFields, $conditions);
	}
}